// PageDateRange.cpp : implementation file
//

#include "stdafx.h"

#include "finance.h"
#include "CDataFile.h"

#include "PageDateRange.h"
#include "financedef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPageDateRange property page

IMPLEMENT_DYNCREATE(CPageDateRange, CPropertyPage)

CPageDateRange::CPageDateRange() : CPropertyPage(CPageDateRange::IDD)
{
	//{{AFX_DATA_INIT(CPageDateRange)
	m_BeginDate = 0;
	m_EndDate = 0;
	//}}AFX_DATA_INIT
}

CPageDateRange::~CPageDateRange()
{
}

void CPageDateRange::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageDateRange)
	DDX_Control(pDX, IDC_SPIN_MONTH, m_NorSpinBtn);
	DDX_Control(pDX, IDC_SPIN_FINANCE, m_FinSpinBtn);
	DDX_DateTimeCtrl(pDX, IDC_DTP_BEGIN, m_BeginDate);
	DDX_DateTimeCtrl(pDX, IDC_DTP_END, m_EndDate);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageDateRange, CPropertyPage)
	//{{AFX_MSG_MAP(CPageDateRange)
	ON_BN_CLICKED(IDC_RDO_ALL, OnRdoAll)
	ON_BN_CLICKED(IDC_RDO_FINANCE, OnRdoFinance)
	ON_BN_CLICKED(IDC_RDO_MONTH, OnRdoMonth)
	ON_BN_CLICKED(IDC_RDO_USER, OnRdoUser)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_FINANCE, OnFinanceMonthUpDown)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_MONTH, ONNormalMonthUpDown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageDateRange message handlers

// [date range]
// range type = { all, finance, month, user }
// begin date = 
// end date = 
//------------------------------------------------------------
BOOL CPageDateRange::OnInitDialog() 
{
	CSpinButtonCtrl*	pWnd = NULL;
	CDateTimeCtrl*		pTCWnd = NULL;
	CButton		*pItemRadio;
	//RANGETYPE	 DRtype;
	CDataFile	 MoneyDF( MONEY_CFG );
	INT			 IDrange[] = {
								IDC_RDO_ALL,
								IDC_RDO_FINANCE,
								IDC_RDO_MONTH,
								IDC_RDO_USER
							};
	
	
	CPropertyPage::OnInitDialog();
	
	// <> Read ini file, then initilize controls
	m_iType = MoneyDF.GetInt( "range type", "Date range" );	

	// <>  range type
	pItemRadio = (CButton*)GetDlgItem( IDrange[m_iType] );
	if( pItemRadio ) 
	{
		pItemRadio->SetCheck( 1 );
		pItemRadio->SetFocus();
	}

	if( m_iType != 3 ) 
	{
		pTCWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_BEGIN );
		if( pTCWnd ) 
			pTCWnd->EnableWindow( FALSE );

		pTCWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_END );
		if( pTCWnd ) 
			pTCWnd->EnableWindow( FALSE );
	}
	
	switch( m_iType ) 
	{
	case 0:	// all
	case 3:	// user
		pWnd = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
		if( pWnd ) 
			pWnd->EnableWindow( FALSE );
		
		pWnd = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
		if( pWnd ) 
			pWnd->EnableWindow( FALSE );
		break;

	case 1:	// Finance
		pWnd = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
		if( pWnd ) 
			pWnd->EnableWindow( TRUE );
		
		pWnd = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
		if( pWnd ) 
			pWnd->EnableWindow( FALSE );
		break;

	case 2:	// Month
		pWnd = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
		if( pWnd ) 
			pWnd->EnableWindow( FALSE );
		
		pWnd = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
		if( pWnd ) 
			pWnd->EnableWindow( TRUE );
		break;
	}

	// <> begin date and end date
	m_BeginDate = MoneyDF.GetInt( "begindate", "Date range" );
	m_EndDate = MoneyDF.GetInt( "enddate", "Date range" );

	
	/*
	m_FinSpinBtn.SetRange( 1, 1500 );
	m_FinSpinBtn.SetBase( 10 );
	m_FinSpinBtn.SetPos( 0 );
	m_NorSpinBtn.SetRange( 1, 1500 );
	m_NorSpinBtn.SetBase( 10 );
	m_NorSpinBtn.SetPos( 0 );*/
	
	

	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//
//------------------------------------------------------------
void CPageDateRange::OnRdoAll() 
{
	CTime		cfgTime_b( 1975, 10, 15, 12, 0, 0 );
	CTime		cfgTime( 2009, 12, 31, 0, 0, 0 );
	time_t		t_b = cfgTime_b.GetTime();
	time_t		t = cfgTime.GetTime();
	CDateTimeCtrl*  pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_BEGIN );
	CSpinButtonCtrl*	pSBC = NULL;
	
	if( pWnd ) 
		pWnd->EnableWindow( FALSE );

	pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_END );
	if( pWnd ) 
		pWnd->EnableWindow( FALSE );

	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
	if( pSBC ) 
		pSBC->EnableWindow( FALSE );

	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
	if( pSBC ) 
		pSBC->EnableWindow( FALSE );
	

	m_iType = 0; // all
	m_BeginDate = t_b;
	m_EndDate = t;

	UpdateData( FALSE );
}

//
// Finance range is separated by FINANCEDAY day
// Such as : 2005/07/10 ~ 2005/08/09 ; date 10 is salaxy day
//------------------------------------------------------------
void CPageDateRange::OnRdoFinance() 
{
	INT			iYear, iMonth, iDate;
	INT			iFBYear, iFBMonth, iFEYear, iFEMonth;
	CTime		curTime = CTime::GetCurrentTime();
	CDateTimeCtrl*  pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_BEGIN );
	CSpinButtonCtrl*	pSBC = NULL;
	
	if( pWnd ) 
		pWnd->EnableWindow( FALSE );
	
	pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_END );
	if( pWnd ) 
		pWnd->EnableWindow( FALSE );
	
	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
	if( pSBC ) 
		pSBC->EnableWindow( TRUE );
	
	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
	if( pSBC ) 
		pSBC->EnableWindow( FALSE );

	m_iType = 1; // finance
	
	iYear	= curTime.GetYear();
	iMonth	= curTime.GetMonth();
	iDate	= curTime.GetDay();
	
	iFBYear = iFEYear = iYear;
	if( iDate < FINANCEDAY ) 
	{
		if( iMonth > 1 ) 
			iFBMonth = iMonth - 1;
		else 
		{
			iFBYear = iYear - 1;
			iFBMonth = 12;
		}
		iFEMonth = iMonth;
	}
	else
	{
		iFBMonth = iMonth;
		if( iMonth < 12 ) 
			iFEMonth = iMonth + 1;
		else
		{
			iFEYear = iYear + 1;
			iFEMonth = 1;
		}
	}

	CTime	tBegin( iFBYear, iFBMonth, FINANCEDAY, 0, 0, 0 );
	CTime	tEnd( iFEYear, iFEMonth, FINANCEDAY - 1, 0, 0, 0 );
	
	m_BeginDate = tBegin.GetTime();
	m_EndDate = tEnd.GetTime();
	
	UpdateData( FALSE );
}

//
// Normal month : 1 ~ month end
//------------------------------------------------------------
void CPageDateRange::OnRdoMonth() 
{
	INT			iYear, iMonth, iDate;
	CTime		curTime = CTime::GetCurrentTime();
					//	Month : 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12
	INT			iMdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	CDateTimeCtrl*  pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_BEGIN );
	CSpinButtonCtrl*	pSBC = NULL;
	
	if( pWnd ) 
		pWnd->EnableWindow( FALSE );
	
	pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_END );
	if( pWnd ) 
		pWnd->EnableWindow( FALSE );
	
	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
	if( pSBC ) 
		pSBC->EnableWindow( FALSE );
	
	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
	if( pSBC ) 
		pSBC->EnableWindow( TRUE );

	m_iType = 2; // month

	iYear	= curTime.GetYear();
	iMonth	= curTime.GetMonth();
	iDate	= iMdays[iMonth];

	if( 2 == iMonth ) 
	{
		if( ( iYear % 4 == 0 && iYear % 100 != 0 ) 
			|| ( iYear % 400 == 0 ) )
		{
			// leap year, max date is 29
			iDate++;
		}
	}

	CTime	tBegin( iYear, iMonth, 1, 0, 0, 0 );
	CTime	tEnd( iYear, iMonth, iDate, 0, 0, 0 );

	m_BeginDate = tBegin.GetTime();
	m_EndDate = tEnd.GetTime();
	
	UpdateData( FALSE );
}

//
// date range by user select
//------------------------------------------------------------
void CPageDateRange::OnRdoUser() 
{
	CDateTimeCtrl*  pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_BEGIN );
	CSpinButtonCtrl*	pSBC = NULL;
	
	if( pWnd ) 
		pWnd->EnableWindow( TRUE );
	
	pWnd = (CDateTimeCtrl*)GetDlgItem( IDC_DTP_END );
	if( pWnd ) 
		pWnd->EnableWindow( TRUE );

	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_FINANCE );
	if( pSBC ) 
		pSBC->EnableWindow( FALSE );
	
	pSBC = (CSpinButtonCtrl*)GetDlgItem( IDC_SPIN_MONTH );
	if( pSBC ) 
		pSBC->EnableWindow( FALSE );
	
	m_iType = 3; // user
}


//
// 
//------------------------------------------------------------
BOOL CPageDateRange::OnApply() 
{
	
	if( 3 == m_iType ) 
	{
		UpdateData( TRUE );

		if( m_BeginDate.GetTime() > m_EndDate.GetTime() )
		{
			MessageBox( "Begin date is more later than End date!", "Warning", MB_ICONWARNING );
			return FALSE;
		}
		
	}

	SaveDateRangeSettings();
	
	return CPropertyPage::OnApply(); // will invoke CPageDateRange::OnOK()
}


//
// 
//------------------------------------------------------------
void CPageDateRange::OnOK() 
{
	if( 3 == m_iType ) 
	{
		UpdateData( TRUE );

		if( m_BeginDate.GetTime() > m_EndDate.GetTime() )
		{
			MessageBox( "Begin date is more later than End date!", "Warning", MB_ICONWARNING );
			return ;
		}
	}

	SaveDateRangeSettings();
	
	CPropertyPage::OnOK();
}

// 
//------------------------------------------------------------
void CPageDateRange::SaveDateRangeSettings()
{
	CDataFile	MoneyDF( MONEY_CFG );
	CHAR	   *chComment[] = {
								"range type is all",	 // 0
								"range type is finance", // 1
								"range type is month",   // 2
								"range type is user"     // 3
							};
	
	
	MoneyDF.SetInt( "range type", m_iType, chComment[m_iType], "Date range" );
	MoneyDF.SetInt( "begindate", m_BeginDate.GetTime(), "", "Date range" );
	MoneyDF.SetInt( "enddate", m_EndDate.GetTime(),  "", "Date range" );
	
	MoneyDF.Save();
}

//
// 
//------------------------------------------------------------
void CPageDateRange::OnFinanceMonthUpDown(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* 	pNMUpDown = (NM_UPDOWN*)pNMHDR;
		
	
	ASSERT( pNMUpDown->iDelta != 0 );

	// 	
	SetFinanceMonth( pNMUpDown->iDelta );
	
	*pResult = 0;
}


//
// 
//------------------------------------------------------------
void CPageDateRange::ONNormalMonthUpDown(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN*	pNMUpDown = (NM_UPDOWN*)pNMHDR;
	INT			i = 0;
	//CHAR		chTmp[100] = {0};
	
	
	ASSERT( pNMUpDown->iDelta != 0 );

	//sprintf( chTmp, "pNMUpDown->iDelta = %d\n", pNMUpDown->iDelta );
	//OutputDebugString( chTmp );

	// 
	SetNorMonth( pNMUpDown->iDelta );
	
	
	*pResult = 0;
}

//
// 
//---------------------------------------------------------------------------------------------
void CPageDateRange::SetFinanceMonth(INT iDelta)
{
	INT			iYear, iMonth;
	INT			iFBYear, iFBMonth, iFEYear, iFEMonth;
	

	if( m_iType != 1 ) // month
		return ;

	// <1> Get current year-month
	UpdateData( TRUE );
	
	iYear	= m_BeginDate.GetYear();
	iMonth	= m_BeginDate.GetMonth();

	// <2> Increase or Reduce 
	if( iDelta < 0 ) // maybe -1, -5, -20
	{
		if( iMonth <= 1 ) 
		{
			iMonth = 12;
			iYear--;
		}
		else
			iMonth--;
	}
	else if( iDelta > 0 )	// maybe 1, 5, 20
	{
		if( iMonth >= 12 ) 
		{
			iMonth = 1;
			iYear++;
		}
		else
			iMonth++;
	}
	
	if( iYear < 1975 ) 
	{
		iYear = 1975;
		iMonth = 1;
	}
	else if( iYear > 2099 ) 
	{
		iYear = 2099;
		iMonth = 12;
	}
	
	
	iFBYear = iFEYear = iYear;
	iFBMonth = iMonth;
	iFEMonth = iFBMonth + 1;
	
	if( iFBMonth == 12 ) 
	{
		iFEMonth = 1;
		iFEYear = iFBYear + 1;
	}
	
	

	CTime	tBegin( iFBYear, iFBMonth, FINANCEDAY, 0, 0, 0 );
	CTime	tEnd( iFEYear, iFEMonth, FINANCEDAY - 1, 0, 0, 0 );
	
	m_BeginDate = tBegin.GetTime();
	m_EndDate = tEnd.GetTime();
	
	UpdateData( FALSE );
	
}

//
// 
//---------------------------------------------------------------------------------------------
void CPageDateRange::SetNorMonth(INT iDelta)
{
	INT			iYear, iMonth, iDate;
					//	Month : 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12
	INT			iMdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	
	
	if( m_iType != 2 ) // month
		return ;

	// <1> Get current year-month
	UpdateData( TRUE );

	iYear	= m_BeginDate.GetYear();
	iMonth	= m_BeginDate.GetMonth();

	// <2> Increase or Reduce 
	if( iDelta < 0 ) // maybe -1, -5, -20
	{
		if( iMonth <= 1 ) 
		{
			iMonth = 12;
			iYear--;
		}
		else
			iMonth--;
	}
	else if( iDelta > 0 )	// maybe 1, 5, 20
	{
		if( iMonth >= 12 ) 
		{
			iMonth = 1;
			iYear++;
		}
		else
			iMonth++;
	}
	
	
	if( iYear < 1975 ) 
	{
		iYear = 1975;
		iMonth = 1;
	}
	else if( iYear > 2099 ) 
	{
		iYear = 2099;
		iMonth = 12;
	}

	iDate	= iMdays[iMonth];

	if( 2 == iMonth ) 
	{
		if( ( iYear % 4 == 0 && iYear % 100 != 0 ) 
			|| ( iYear % 400 == 0 ) )
		{
			// leap year, max date is 29
			iDate++;
		}
	}
	
	CTime	tBegin( iYear, iMonth, 1, 0, 0, 0 );
	CTime	tEnd( iYear, iMonth, iDate, 0, 0, 0 );
	
	m_BeginDate = tBegin.GetTime();
	m_EndDate = tEnd.GetTime();
	
	UpdateData( FALSE );
}

